package com.example.kotlin_springboot.controller

import com.example.kotlin_springboot.entity.Task
import com.example.kotlin_springboot.service.TaskService
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/task")
class TaskController(val service: TaskService) {

    @GetMapping
    fun task() = ResponseEntity.ok(service.getAll())

    @PostMapping
    fun create(@RequestBody task: Task): ResponseEntity<String> {
        val result = service.save(task)
        return ResponseEntity.ok("added task with desc ${result.desc}")
    }

    @DeleteMapping("/{id}")
    fun delete(@PathVariable id: Long) = if (service.delete(id)) {
        ResponseEntity.ok("Task with ${id} deleted")
    } else {
        ResponseEntity.status(404).body("Task with ${id} not found")
    }
}